//
//  TMQuiltView
//
//  Created by Bruno Virlet on 7/20/12.
//
//  Copyright (c) 2012 1000memories

//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
//  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//  DEALINGS IN THE SOFTWARE.
//


#import "TMPhotoQuiltViewCell.h"
#import "UIImageView+WebCache.h"

const CGFloat kTMPhotoQuiltViewMargin = 5;
const CGFloat kTMFooterViewHeight = 15;
const CGFloat kTMTextViewHeight = 20;

@implementation TMPhotoQuiltViewCell

@synthesize photoView = _photoView;
@synthesize titleLabel = _titleLabel;
@synthesize nameLabel = _nameLabel;
@synthesize timeLabel = _timeLabel;

- (void)dealloc {
    [_photoView release], _photoView = nil;
    [_titleLabel release], _titleLabel = nil;
    [_nameLabel release]; _nameLabel = nil;
    [_timeLabel release]; _timeLabel = nil;
    
    [super dealloc];
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (UIImageView *)photoView {
    self.photoView = [(UIImageView*)[self viewWithTag:1001] retain];
    if (!_photoView) {
        _photoView = [[UIImageView alloc] init];
        _photoView.contentMode = UIViewContentModeScaleAspectFill;
        _photoView.clipsToBounds = YES;
        _photoView.tag = 1001;
        [self addSubview:_photoView];
    }
    return _photoView;
}

- (UILabel *)titleLabel {
    _titleLabel = [(UILabel*)[self.photoView viewWithTag:1002] retain];
    if (!_titleLabel) {
        UIColor *backColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        _titleLabel = [[UIView createLabel:@"" font:12.0f isBold:YES] retain];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = backColor;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.tag = 1002;
        [self.photoView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)nameLabel {
    _nameLabel = [(UILabel*)[self viewWithTag:1003] retain];
    if (!_nameLabel) {
        _nameLabel = [[UIView createLabel:@"" font:12.0f isBold:YES] retain];
        _nameLabel.textColor = [UIColor lightGrayColor];
        _nameLabel.tag = 1003;
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)timeLabel {
    _timeLabel = [(UILabel*)[self viewWithTag:1004] retain];
    if (!_timeLabel) {
        _timeLabel = [[UIView createLabel:@"" font:12.0f isBold:YES] retain];
        _timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.tag = 1004;
        [self addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UIButton*)likeView {
    _likeView = [(UIButton*)[self viewWithTag:1005] retain];
    if (!_likeView) {
        _likeView = [UIButton buttonWithType:UIButtonTypeCustom];
        _likeView.top = 2;
        _likeView.width = 40;
        _likeView.height = 15;
        [_likeView setTitleForNormal:@"0"];
        _likeView.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        _likeView.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [_likeView setBackgroundNormal:@"like_btn.png"];
        _likeView.tag = 1005;
        [self.photoView addSubview:_likeView];
    }
    return _likeView;
}

- (UIButton*)delButton {
    _delButton = [(UIButton*)[self viewWithTag:1006] retain];
    if (!_delButton) {
        _delButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        _delButton.top = 0;
        _delButton.left = 0;
        _delButton.width = 40;
        _delButton.height = 20;
        [_delButton setTitleForNormal:@"删除"];
        [_delButton setTitleColorNormal:[UIColor grayColor]];
        _delButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
//        _delButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        _delButton.tag = 1006;
        [_delButton addTarget:self action:@selector(delPhotoAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_delButton];
    }
    return _delButton;
}

- (void)delPhotoAction:(id)sender
{
    if (_delegate) {
        [_delegate deleteDidSelect:_dictionary];
    }
}

- (void)setContent:(NSDictionary*)dic
{
    self.dictionary = dic;
    NSURL *url = [NSURL URLWithString:[dic objectForKey:@"image"]];
    NSString *title = [dic objectForKey:@"pinglun"];
    NSString *name = [dic objectForKey:@"pname"];
    NSString *ftime = [dic objectForKey:@"ftime"];
    NSString *like = [dic objectForKey:@"zan"];
    
    [self.photoView setImageWithURL:url];
    
    self.titleLabel.text = title;
    self.nameLabel.text = name;
    self.timeLabel.text = ftime;
    [self.likeView setTitleForNormal:like];
    
    [self reLayoutSubviews];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self reLayoutSubviews];
}

- (void)reLayoutSubviews
{
    [self drawBorder];
    
    self.photoView.left = self.photoView.top = kTMPhotoQuiltViewMargin;
    self.photoView.width = self.width-kTMPhotoQuiltViewMargin * 2;
    self.photoView.height = self.height-kTMPhotoQuiltViewMargin * 2 - kTMFooterViewHeight;
    
    self.likeView.left = self.photoView.width - self.likeView.width-2;
    
    self.titleLabel.left = 0;
    self.titleLabel.width = self.photoView.width;
     self.titleLabel.height = 20;
    self.titleLabel.top = self.photoView.height - self.titleLabel.height;
    self.titleLabel.width = self.photoView.width;
    
    self.nameLabel.left = self.photoView.left;
    self.nameLabel.top = self.timeLabel.top = self.photoView.bottom_2;
    [self.nameLabel autoLabelSize];
    
    if (_isUserCenter) {
        self.delButton.left = 0;
        self.delButton.top = self.photoView.bottom;
        self.nameLabel.left = self.delButton.right_2;
    }
    
    [self.timeLabel autoWidthForSingleLine];
    [self.timeLabel autoHeight];
    self.timeLabel.right = self.photoView.right;
}

@end
